var util = require("../../utils/util.js");
var app = getApp();
Page({
  data: {
    tempFilePaths: '',
    formData: null,
    isNone: false,
    isBlock: true,
    shopname: "",
    location: {},
    imgUrl: "",
    aaa:null,
    array:null,
    checkbox:[0],
    currentId:null,
    arr:[],
    typeShop:"",
    index1:0,
    index2:0,
    index3:0,
    provices:null,
    citys:null,
    areas:null,
    currentAddress:[]
  },
  insert: function() {
    var cb = this.data.checkbox;
    cb.push(this.data.checkbox.length);
    this.setData({
      checkbox: cb
    });
  },
  minusBox: function() {
    var cb = this.data.checkbox;
    cb.splice(this.data.checkbox.length-1, 1);
    var ddd = this.data.arr
    ddd.splice(this.data.arr.length-1, 1)
    this.setData({
      checkbox: cb,
      arr:ddd
    });
    
  },
  listenerPickerSelected: function(e) {
    
      //改变index值，通过setData()方法重绘界面
      var arr1 = this.data.arr
      arr1.push(this.data.array[e.detail.value])
      this.setData({
        // currentId:e.detail.target.id,
        arr: arr1,
        index:e.detail.value
      });
  },
  listenerPickerSelected1: function (e){
    var that = this
      var provinceId = this.data.provices.provinceid[e.detail.value]
      // var 
      wx.request({
        url: 'http://1.aiym.applinzi.com/aiyouwai.php',
        data: {
          type:"getCitys",
          paras:{
            provinceid:provinceId
          }
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res){
          // success
          // console.log(res.data)
          that.setData({
            citys:res.data
          })
        }
      })
      that.setData({
        index1:e.detail.value
      });

  },
  listenerPickerSelected2: function (e){
      var that = this
      var cityid = this.data.citys.cityid[e.detail.value]
      console.log(this.data.citys)
      wx.request({
        url: 'http://1.aiym.applinzi.com/aiyouwai.php',
        data: {
          type:"getAreas",
          paras:{
            cityid:cityid
          }
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res){
          // success
          console.log(res.data)
          that.setData({
            areas:res.data
          })
        }
      })
      that.setData({
        index2:e.detail.value
      });
  },
  listenerPickerSelected3: function (e){
      this.setData({
        index1:e.detail.value
      });
  },
  focus:function (e){
    console.log(e)
  },
  onLoad: function () {

    util.location(this)  // 获取商家位置
    var that = this
    // 加载时先获取省名信息
    wx.request({
      url: 'http://1.aiym.applinzi.com/aiyouwai.php',
      data: {
        type:"getProvices",
        paras:{}
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        console.log(res)
        that.setData({
          provices:res.data
        })
      }

    })


    wx.request({
      url: 'http://1.aiym.applinzi.com/aiyouwai.php',
      data: {
        type:"firstPage"
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        // console.log(res.data.length)
        var arr = []
        for(var i=0; i<res.data.length; i++){
            arr.push(res.data[i].content)
        }
        that.setData({
          array:arr
        })
      }
    })
  },
  chooseimage: function () {
    util.chooseImg(this)  // 选择图片
  },

  formSubmit: function (e) {  
    var that = this;
    that.setData({
      formData: e.detail.value,   // 获取商家注册第一步的表单信息，并赋值给全局变量
      typeShop:that.data.arr.join('|')
    })
    // console.log(that.data.typeShop)
    var flag = util.checkPhoneNum(e.detail.value.phoneNum, that) && util.checkPassword(e.detail.value.password, that)
    // var flag = false
    if (flag) {
      wx.request({
        url: 'http://1.aiym.applinzi.com/aiyouwai.php',
        data: {
          type: "businessRegist",
          paras: this.data.formData
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          // that.modalTap();
          if (res.data.errcode == 0) {
            that.setData({
              imgUrl:""
            })
            wx.showModal({
              title: "提示",
              content: "恭喜你，进入下一步",
              success: function () {
                that.setData({
                  isNone: true,
                  isBlock: false,
                  imgUrl: 'http://',
                  shopname: that.data.formData.phoneNum
                })
              }
            })
          } else if (res.data.errcode == 1) {
            wx.showToast({
              title: "该手机号已注册",
              duration: 500
            })
          }
        }
      })
    }
  },
  infor: function (e) {  // 商家注册第二部
    var that = this;
    that.setData({
      formData: e.detail.value
    })
    console.log(e)
    var flag = util.checkId(e.detail.value.IDcard) && util.checkBankcard(e.detail.value.bankcard)
    if (flag) {
      wx.request({
        url: 'http://1.aiym.applinzi.com/aiyouwai.php',
        data: {
          type: "businessRegistNext",
          paras: this.data.formData
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res);
          if (res.data.errcode == 0) {
            wx.showModal({
              title: "提示",
              content: "注册成功",
              success: function () {
                wx.navigateTo({
                  url: "../login/login" // 回退前 delta(默认为1) 页面
                })
                wx.setStorage({
                  key: 'userInfo',
                  data: null
                })
              }
            })
          } else if (res.data.errcode == 1) {
            console.log("已注册");
          }
        }
      })
    }
  },
  onshow: function () {
    this.setData({
      tempFilePaths: ""
    })
  }
  
})